import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import electron from "vite-plugin-electron/simple";
import renderer from 'vite-plugin-electron-renderer'

import path from 'node:path'

// https://vite.dev/config/
export default defineConfig({
  base: "./",
  build: {
    emptyOutDir: false
  },
  plugins: [
    vue(),
    electron({
      main: {
        // 配置 Electron 入口文件
        entry: path.join(__dirname, "src-electron/main.ts"),
      },
      preload: {
        // 配置 Electron 预加载脚本
        input: path.join(__dirname, "src-electron/preload.ts"),
      },
      // Ployfill the Electron and Node.js API for Renderer process.
      // If you want use Node.js in Renderer process, the `nodeIntegration` needs to be enabled in the Main process.
      // See 👉 https://github.com/electron-vite/vite-plugin-electron-renderer
      renderer: process.env.NODE_ENV === 'test'
        // https://github.com/electron-vite/vite-plugin-electron-renderer/issues/78#issuecomment-2053600808
        ? undefined
        : {},
    }),
    // https://github.com/electron-vite/vite-plugin-electron-renderer
    // renderer({}),
    renderer(),
  ],
  server: {
    port: 1420,
    strictPort: true,
    host:'0.0.0.0',
    cors:true,
    hmr:true,
    open: true,
    watch: {
      // 3. tell vite to ignore watching `src-tauri`
      ignored: ["**/src-tauri/**"],
    },
    proxy: {
      '/api': {
        target: 'http://192.168.100.223:86',
        changeOrigin: true,
        rewrite: (p) => p.replace(/^\/api/, '/api')
      },
      // https://cn.vitejs.dev/config/#server-proxy
      '/dev-api': {
        target: 'http://192.168.100.223:86',
        changeOrigin: true,
        rewrite: (p) => p.replace(/^\/dev-api/, '/api')
      },
      '/prod-api': {
        target: 'http://192.168.100.223:86',
        changeOrigin: true,
        rewrite: (p) => p.replace(/^\/prod-api/, '/api')
      }
    }
  },
  envPrefix: [
    "VITE_",
    "TAURI_PLATFORM",
    "TAURI_ARCH",
    "TAURI_FAMILY",
    "TAURI_PLATFORM_VERSION",
    "TAURI_PLATFORM_TYPE",
    "TAURI_DEBUG",
  ],
  css: {
    postcss: {
      plugins: [
        {
          postcssPlugin: 'internal:charset-removal',
          AtRule: {
            charset: (atRule) => {
              if (atRule.name === 'charset') {
                atRule.remove();
              }
            }
          }
        }
      ]
    }
  }
});
